package com.jtech.together.view.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jtech.together.R;
import com.jtech.together.model.FriendModel;
import com.jtech.together.model.UserModel;
import com.jtech.together.util.ImageUtils;
import com.jtechlib2.adapter.RecyclerAdapter;
import com.jtechlib2.view.RecyclerHolder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 好友列表适配器
 * Created by wuxubaiyang on 2017/5/12.
 */

public class FriendsAdapter extends RecyclerAdapter<FriendModel> {
    private Map<String, Integer> keyWordMap;

    public FriendsAdapter(Context context) {
        super(context);
    }

    /**
     * 获取索引关键字第一次显示的位置
     *
     * @param keyWord
     * @return
     */
    public int getKeyWordFirstShowPosition(String keyWord) {
        if (null == keyWordMap || null == keyWordMap.get(keyWord)) {
            return 0;
        }
        return keyWordMap.get(keyWord);
    }

    /**
     * 移除一个好友
     *
     * @param keyWord
     * @param position
     */
    public void removeFriend(String keyWord, final int position) {
        removeData(position);
        removeKeyWord(keyWord, position);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                notifyItemChanged(position);
            }
        }, 400);
    }

    /**
     * 移除一个键
     *
     * @param keyWord
     * @param position
     */
    public void removeKeyWord(String keyWord, int position) {
        Integer index = keyWordMap.get(keyWord);
        if (null != keyWordMap && null != index) {
            if (index == position) {
                keyWordMap.remove(keyWord);
            }
        }
    }

    @Override
    public void setDatas(Collection<FriendModel> datas) {
        super.setDatas(datas);
        //实例化索引表
        this.keyWordMap = new HashMap<>();
        //遍历集合并处理索引位置
        List<FriendModel> friendList = new ArrayList<>(datas);
        for (int i = 0; i < friendList.size(); i++) {
            FriendModel model = friendList.get(i);
            //当数据为第一条或者当上一条数据与当前数据的关键字不同，则存储记录
            if (i == 0 || !friendList.get(i - 1).getLetter().equals(model.getLetter())) {
                keyWordMap.put(model.getLetter(), i);
            }
        }
    }

    /**
     * 获取好友的objectId
     *
     * @param position
     * @return
     */
    public String getFriendObjectId(int position) {
        return getItem(position)
                .getFriendUser()
                .getObjectId();
    }

    @Override
    protected View createView(LayoutInflater layoutInflater, ViewGroup viewGroup, int viewType) {
        return layoutInflater.inflate(R.layout.view_friend_item, viewGroup, false);
    }

    @Override
    protected void convert(RecyclerHolder holder, FriendModel model, int position) {
        //获取好友对象
        UserModel friend = model.getFriendUser();
        //设置关键字
        holder.setText(R.id.textview_friend_keyword, model.getLetter());
        //设置是否显示关键字(与上一条数据判断，如果不同则显示)
        holder.setViewVisible(R.id.textview_friend_keyword,
                position == 0 || !getItem(position - 1).getLetter().equals(model.getLetter()));
        //显示用户头像
        ImageUtils.showCircleImage(getContext(), friend.getAvatar(), holder.getImageView(R.id.imageview_friend_avatar),
                R.drawable.ic_default_avatar, R.drawable.ic_default_avatar);
        //设置用户昵称
        holder.setText(R.id.textview_friend_nickname, friend.getNickname());
        //设置用户信息
        holder.setText(R.id.textview_friend_info, friend.getInfo());
    }
}